<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\abnormalorder\controller;

class AdminProcess extends \app\AdminRest
{
    protected $model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\abnormalorder\model\OrderProcess();
    }
    public function getProcessStartInfo()
    {
        $role_model = new \app\node\model\RoleList();
        $admin_model = new \app\massage\model\Admin();
        $admin_id = $role_model->getShopOrderRole($this->_uniacid);
        $data = $admin_model->where(["status" => 1, "uniacid" => $this->_uniacid])->where("id", "in", $admin_id)->field("id,agent_name")->select()->toArray();
        return $this->success($data);
    }
    public function processList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", ">", -1];
        $dis[] = ["type", "=", $input["type"]];
        $data = $this->model->where($dis)->order("top,id desc")->select()->toArray();
        $have = $this->model->where(["type" => $input["type"], "uniacid" => $this->_uniacid, "status" => 1])->find();
        $arr["can_stop"] = !empty($have) ? 1 : 0;
        $arr["list"] = $data;
        return $this->success($arr);
    }
    public function stopProcess()
    {
        $input = $this->_input;
        $res = $this->model->dataUpdate(["uniacid" => $this->_uniacid, "type" => $input["type"], "status" => 1], ["status" => 0]);
        return $this->success($res);
    }
    public function processAdd()
    {
        $input = $this->_input;
        $role_model = new \app\abnormalorder\model\OrderRole();
        $wander_model = new \app\abnormalorder\model\OrderWander();
        $this->model->dataUpdate(["uniacid" => $this->_uniacid, "type" => $input["type"]], ["status" => -1]);
        \think\facade\Db::startTrans();
        foreach ($input["data"] as $key => $value) {
            $insert = ["uniacid" => $this->_uniacid, "top" => $value["top"], "deduct_status" => $value["deduct_status"], "type" => $input["type"], "sub_type" => $value["sub_type"], "status" => 1];
            if(!empty($value["id"])) {
                $this->model->dataUpdate(["id" => $value["id"]], $insert);
                $id = $value["id"];
            } else {
                $this->model->dataAdd($insert);
                $id = $this->model->getLastInsID();
            }
            $role_model->where(["process_id" => $id])->delete();
            $role_insert = [];
            foreach ($value["role"] as $ks => $vs) {
                $role_insert[$ks] = ["uniacid" => $this->_uniacid, "process_id" => $id, "type" => $vs["type"], "user_id" => $vs["user_id"], "process_type" => $input["type"]];
            }
            $role_model->saveAll($role_insert);
            $wander_model->where(["process_id" => $id])->delete();
            $wander_model->wanderAdd($value["wander"], $this->_uniacid, $id);
        }
        \think\facade\Db::commit();
        return $this->success(true);
    }
}

?>